package com.neuedu.dao;



import com.neuedu.entity.Menu;
import com.neuedu.utils.DBUtils;

import java.util.List;
import java.util.Map;

public class MenuDao {

    /**
     * 根据条件查询集合
     * @param where
     * @param params
     * @return
     */
    //where  =      where sss = ?
    public List<Menu> selectList(String where , Object ... params){
        String sql =" select * from menu ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        List<Menu> menus = DBUtils.selectList(Menu.class, sql, params);

        return menus;
    }

    /**
     * 分页查询
     * @param where
     * @param pageNo
     * @param size
     * @param params
     * @return
     */
    public List<Menu> selectPage(String where , int pageNo,int size ,Object ... params){
        String sql =" select * from menu ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql += " limit "+(pageNo-1)*size+"   ,"+size;

//        System.out.println("sql = " + sql);
        List<Menu> menus = DBUtils.selectList(Menu.class, sql, params);

        return menus;
    }


    /**
     * 根据条件查询一个
     * @param where
     * @param params
     * @return
     */
    public Menu selectOne(String where ,Object ... params){
        String sql =" select * from menu ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        return DBUtils.selectOne(Menu.class, sql, params);
    }


    /**
     * 查询总条数
     * @param where
     * @param params
     * @return
     */
    public long selectCount(String where ,Object ... params){
        String sql =" select * from menu ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql = "select count(1) count from ("+ sql  +") tab   ";
        Map map = DBUtils.selectMap( sql , params);
        long count = (long) map.get("count");
        return count;
    }

    /**
     * 根据数据进行插入
     * @param menu
     * @return
     */
    public int insert(Menu menu){
        String sql = "INSERT INTO `menu` (    `menu_name`, `url`, `parent_id` )  VALUES  ( ?,?,? )  ";
        return DBUtils.update( sql,
                menu.getMenuName(),
                menu.getUrl(),
                menu.getParentId()   );
    }

    /**
     * 根据主键更新一条
     * @param menu
     * @return
     */
    public int updateById(Menu menu){
        String sql = "UPDATE   `menu`  SET `menu_name` = ?, `url` = ?, `parent_id` = ?, `active` = ?, `createtime` = ? WHERE `menu_id` =  ?  ";
        return DBUtils.update( sql,
                menu.getMenuName(),
                menu.getUrl(),
                menu.getParentId() ,
                menu.getActive(),
                menu.getCreatetime(),
                menu.getMenuId());

    }


    /**
     * 根据主键更新一条
     * @param id
     * @return
     */
    public int deleteById(Integer id){
        String sql = " update menu set active = 0    where menu_id = ?";
        return DBUtils.update( sql, id);

    }


    /**
     * 根据条件删除
     * @param where
     * @param params
     * @return
     */
    public int delete(String where ,Object... params){
        String sql = " delete from menu ";
        if(where == null || "".equals(where)){
            throw new RuntimeException("必须有条件删除");
        }
        sql += where;
        return DBUtils.update( sql, params);

    }

}
